package com.wlyuan.open.scanner.assembler;

import com.wlyuan.core.domain.MappingIds;
import com.wlyuan.open.dto.OpenTemplateSyncDTO;
import com.wlyuan.open.scanner.domain.template.Template;
import com.wlyuan.open.scanner.utils.DateUtils;
import com.wlyuan.open.scanner.utils.enums.quotation.TemplateMapping;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author lijing
 * @description
 */
public class ScannerTemplateAssembler {

    private static final Assembler ASSEMBLER = Mappers.getMapper(Assembler.class);

    @Mapper(uses = {TemplateMapping.class, MappingIds.class})
    public interface Assembler {

        /**
         * 转换
         *
         * @param source
         * @return
         */
        @Mappings({
                @Mapping(source = "createTime", target = "createAt"),
                @Mapping(source = "updateTime", target = "updateAt"),
        })
        OpenTemplateSyncDTO assemble(Template source);

        @AfterMapping
        default void after(@MappingTarget OpenTemplateSyncDTO target) {
            if (target.getStartTime() == null) {
                target.setStartTime(DateUtils.defaultDateTime());
            }
        }

        /**
         * 转换
         *
         * @param source
         * @return
         */
        List<OpenTemplateSyncDTO> assemble(List<Template> source);

    }

    public static List<OpenTemplateSyncDTO> assemble(List<Template> creation) {
        return ASSEMBLER.assemble(creation);
    }

    public static OpenTemplateSyncDTO assemble(Template creation) {
        return ASSEMBLER.assemble(creation);
    }

}
